Skip to content

perf(build): improve tree shaking with purity annotations - #3167

Open
camc314 wants to merge 3 commits into
cloudflare:mainfrom
camc314:codex/pure-dynamic-annotations
Open

perf(build): improve tree shaking with purity annotations#3167
camc314 wants to merge 3 commits into
cloudflare:mainfrom
camc314:codex/pure-dynamic-annotations

Conversation

@camc314

@camc314 camc314 commented Sep 3, 2026

Copy link
Copy Markdown
Contributor

Summary

  • annotate eligible transformed next/dynamic calls as pure so Rolldown can remove unused component factories and lazy chunks
  • mark the script nonce hook factory as side-effect-free so consumers that only need withScriptNonce do not retain unused hook setup

Details

The dynamic transform limits the hint to function/import loaders with accessor-free object arguments, and preserves existing user or compiler annotations without duplication.

Build-size probe

Isolated minified Rolldown production bundles with React externalized, totaling all emitted JavaScript chunks:

Scenario Before After Reduction
Unused next/dynamic component 5,466 B / 2,341 B gzip 163 B / 142 B gzip 97.0% / 93.9% gzip
withScriptNonce-only import 405 B / 236 B gzip 296 B / 203 B gzip 26.9% / 14.0% gzip

These focused probes isolate the tree-shaking opportunity; whole-application savings depend on which exports and dynamic components are unused.

Copilot AI lite review requested due to automatic review settings September 3, 2026 10:59
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Sep 3, 2026

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review Completed 2026-09-03T11:02:27.489796Z 0602f72 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@pkg-pr-new

pkg-pr-new Bot commented Sep 3, 2026

Copy link
Copy Markdown

Open in StackBlitz

npm i https://pkg.pr.new/@vinext/cloudflare@3167
npm i https://pkg.pr.new/create-vinext-app@3167
npm i https://pkg.pr.new/@vinext/types@3167
npm i https://pkg.pr.new/vinext@3167

commit: f539292

@github-actions

github-actions Bot commented Sep 3, 2026

Copy link
Copy Markdown
Contributor

Performance benchmarks

Compared f539292 against base f60afed using alternating same-runner rounds. Next.js was unchanged and skipped.

1 improved · 0 regressed · 5 within ±1.5%

Scenario Framework Baseline Current Change
Client bundle size (gzip) vinext 142.3 KB 142.3 KB ⚫ +0.0%
Client entry size (gzip) vinext 129.6 KB 129.6 KB ⚫ +0.0%
Dev server cold start vinext 3.17 s 3.12 s 🟢 -1.5%
Production build time vinext 3.61 s 3.59 s ⚫ -0.6%
RSC entry closure size (gzip) vinext 119.2 KB 119.2 KB ⚫ 0.0%
Server bundle size (gzip) vinext 202.8 KB 202.7 KB ⚫ -0.0%

View detailed results and traces

🟢 improvement · 🔴 regression · ⚫ change below 1.5% · paired base/head

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The current @__PURE__ eligibility allows ImportExpression loaders, which can cause bundlers to drop eager import(...) evaluation when the call is removed, changing runtime semantics.

Once you've addressed the issues Copilot identified, you can request another Copilot review.

Pull request overview

This PR improves build-time dead-code elimination by adding “purity”/“no side effects” annotations so Rolldown can more aggressively tree-shake unused next/dynamic component factories and unused script nonce hook setup, reducing retained code and lazy chunks in consumer bundles.

Changes:

  • Annotate eligible transformed next/dynamic call sites with /* @__PURE__ */ during the preload-metadata transform.
  • Mark the script nonce hook factory as side-effect-free to allow removing unused hook setup when only withScriptNonce is imported.
  • Add build-optimization regression tests covering both tree-shaking scenarios and guardrails (no duplicate pure annotations; avoid accessor-based option objects).
File summaries
File Description
tests/build-optimization.test.ts Adds regression tests ensuring unused dynamic calls and unused script nonce hook setup are tree-shaken.
packages/vinext/src/shims/script-nonce-context.tsx Adds a @__NO_SIDE_EFFECTS__ annotation to enable DCE of unused hook setup.
packages/vinext/src/plugins/dynamic-preload-metadata.ts Extends the next/dynamic transform to optionally annotate transformed calls as @__PURE__, with eligibility checks.
Review details
  • Files reviewed: 3/3 changed files
  • Comments generated: 2
  • Review effort level: Lite

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread packages/vinext/src/plugins/dynamic-preload-metadata.ts
Comment thread tests/build-optimization.test.ts Outdated
@james-elicx

Copy link
Copy Markdown
Member

/bigbonk review for issues

@ask-bonk

ask-bonk Bot commented Sep 3, 2026

Copy link
Copy Markdown
Contributor

LGTM!

github run

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants